package com.kawaiior.asoulworld.events;

import com.kawaiior.asoulworld.IdlFramework;
import com.kawaiior.asoulworld.potion.ModPotions;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.DamageSource;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.CriticalHitEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;


@Mod.EventBusSubscriber(modid = IdlFramework.MODID)
public class ASPotionEvent {

    //真实伤害 AVA buff
    @SubscribeEvent
    public static void playerAvaPotionActive(LivingHurtEvent event)
    {
        if (event.getEntity().world.isRemote)
        {
            return;
        }
        if (event.getSource().getTrueSource()==null)
        {
            return;
        }
        if (!(event.getSource().getTrueSource() instanceof EntityLivingBase)){return;}
        EntityLivingBase attaker = (EntityLivingBase) event.getSource().getTrueSource();
        if (attaker.isPotionActive(ModPotions.AS_AVA_BUFF))
        {
            if (event.getAmount() < 6){
                event.setAmount(9f);
            }else {
                event.setAmount(event.getAmount()*1.5f);
            }
            event.getSource().setDamageBypassesArmor();
        }
    }

    //嘉然套 必暴击 OverLoad buff
    @SubscribeEvent
    public static void dianaOverLoadEvent(CriticalHitEvent event)
    {
        if (event.getEntity().world.isRemote)
        {
            return;
        }
        if (event.getEntityLiving()==null)
        {
            return;
        }
        if (event.getEntityLiving().isPotionActive(ModPotions.AS_OVER_LOAD))
        {
            event.setResult(Event.Result.ALLOW);
            event.setDamageModifier(3.0f);
        }
    }

//    //流血
//    /**
//     * 每秒有10%的概率流血
//     * 流血的伤害为当前流血等级对应的数字
//     */
//    @SubscribeEvent
//    public static void playerBleedingEvent(TickEvent.PlayerTickEvent event)
//    {
//        if (event.side.isClient())return;
//        if (new Random().nextFloat() > 0.005254f)return;
//        EntityPlayer player = event.player;
//        if (player.isPotionActive(ModPotions.AS_BLEEDING))
//        {
//            Collection<PotionEffect> activePotionEffects = player.getActivePotionEffects();
//            for (int i = 0; i < activePotionEffects.size(); i++) {
//                PotionEffect buff = (PotionEffect)activePotionEffects.toArray()[i];
//                if (buff.getPotion() instanceof AsBleedingPotion)
//                {
//                    player.attackEntityFrom(AsDamageSource.AS_LIVING_BLEEDING,buff.getAmplifier()+1f);
//                }
//            }
//        }
//    }

    //破防 处于该状态下受到的伤害为真实伤害,并且造成的伤害减少20%
    @SubscribeEvent
    public static void  entityBreakDefense(LivingHurtEvent event)
    {
        if (event.getEntity().world.isRemote)return;
        if (event.getEntityLiving().isPotionActive(ModPotions.AS_BREAK_DEFENSE)){
            event.getSource().setDamageBypassesArmor();
        }
        if (event.getSource().getTrueSource() instanceof EntityLivingBase){
            EntityLivingBase entityLivingBase = (EntityLivingBase) event.getSource().getTrueSource();
            if (entityLivingBase.isPotionActive(ModPotions.AS_BREAK_DEFENSE)){
                event.setAmount(event.getAmount()*0.8f);
            }
        }
    }

    //掉落伤害免疫
    @SubscribeEvent(priority = EventPriority.HIGH)
    public static void entityIgnoreFall(LivingHurtEvent event)
    {
        if (event.getEntity().world.isRemote)return;
        if (event.getEntityLiving().isPotionActive(ModPotions.AS_FALL_IGNORE) && event.getSource() == DamageSource.FALL)
        {
            event.setAmount(0);
            event.setCanceled(true);
        }
    }

}
